package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.EmployeeType;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import database.EmployeeTypeDelegate;

public class EmT_Update extends JDialog implements ActionListener{

	private JTextField txtEmTName;
	private JLabel lblEmTName;
	private JLabel lblEmTDes;
	private JTextArea txaEmTDes;
	private JButton btnSave;
	private JButton btnCancel;
	private JScrollPane scrollPane;
	private int id;
	private int rowCount;
	public String nv1;
	public String name;
	boolean flag1 = false;
	boolean flag2 = false;

	public EmT_Update(JFrame main, String nV,final String mT,final int id1,int row)  {
		super(main, "Sửa Nghiệp Vụ", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		rowCount = row;
		id = id1;
		nv1 = mT;
		name = nV;
		int height = 200;
	    int width = 420;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
		setResizable(false);
	    initComponet();
	}

	public void initComponet(){
	    GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblEmTName = new JLabel("Nghiệp vụ: ");
		lblEmTName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmTName = new GridBagConstraints();
		gbc_lblEmTName.anchor = GridBagConstraints.WEST;
		gbc_lblEmTName.insets = new Insets(15, 15, 5, 5);
		gbc_lblEmTName.fill = GridBagConstraints.VERTICAL;
		gbc_lblEmTName.gridx = 0;
		gbc_lblEmTName.gridy = 0;
		getContentPane().add(lblEmTName, gbc_lblEmTName);
		
		txtEmTName = new JTextField();
		//Event
		txtEmTName.setBackground(UtilityGUI.colorBgOldTxtUpdate);
		txtEmTName.setForeground(UtilityGUI.colorOldTxtUpdate);
		txtEmTName.setFont(UtilityGUI.fontTxtInsert);
		txtEmTName.setText(name);
		txtEmTName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtEmTName.setText(StringUtil.removeUnwantedSpace(txtEmTName.getText()));
			}
		});
		txtEmTName.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				if(txtEmTName.getText().length()==0 || txtEmTName.getText() == null){
					txtEmTName.setText(name);
					txtEmTName.setBackground(UtilityGUI.colorBgOldTxtUpdate);
					txtEmTName.setForeground(UtilityGUI.colorOldTxtUpdate);
				}
			}			
			@Override
			public void focusGained(FocusEvent e) {
				if(txtEmTName.getText().equals(name)){
				txtEmTName.setBackground(UtilityGUI.colorBgNewTxtUpdate);
				txtEmTName.setFont(UtilityGUI.fontTxtInsert);
				txtEmTName.setText("");
				}
			}
		});
		//event check database
		txtEmTName.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				EmployeeTypeDelegate d = new EmployeeTypeDelegate();
				if(txtEmTName.getText()==name){
					btnSave.setEnabled(true);
				}
				else if(!d.CheckInsertEmTName(StringUtil.removeUnwantedSpace(txtEmTName.getText()))&&txtEmTName.getText().length()>3){
					btnSave.setEnabled(true);
				}else {
					btnSave.setEnabled(false);
					
				}
			
			}
		});
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(15, 5, 5, 15);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		getContentPane().add(txtEmTName, gbc_textField);
		txtEmTName.setColumns(10);
		
		
		lblEmTDes = new JLabel("Mô tả: ");
		lblEmTDes.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmTDes = new GridBagConstraints();
		gbc_lblEmTDes.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblEmTDes.insets = new Insets(5, 15, 5, 5);
		gbc_lblEmTDes.gridx = 0;
		gbc_lblEmTDes.gridy = 1;
		getContentPane().add(lblEmTDes, gbc_lblEmTDes);
		
		txaEmTDes = new JTextArea(4,10);
		txaEmTDes.setBackground(UtilityGUI.colorBgOldTxtUpdate);
		txaEmTDes.setForeground(UtilityGUI.colorOldTxtUpdate);
		txaEmTDes.setFont(UtilityGUI.fontTxtInsert);
		txaEmTDes.append(nv1);
		txaEmTDes.addFocusListener(new FocusListener() {	
			@Override
			public void focusLost(FocusEvent e) {
				String a =  "  ";
				a = a.trim();
				if(txaEmTDes.getText().trim().equals(a)){
					txaEmTDes.setText(nv1);
					txaEmTDes.setBackground(UtilityGUI.colorBgOldTxtUpdate);
					txaEmTDes.setForeground(UtilityGUI.colorOldTxtUpdate);
				}
			}			
			@Override
			public void focusGained(FocusEvent e) {
				if(txaEmTDes.getText().equals(nv1)){
				txaEmTDes.setBackground(UtilityGUI.colorBgNewTxtUpdate);
				txaEmTDes.setFont(UtilityGUI.fontTxtInsert);
				txaEmTDes.setText("");
				}
			}
		});
		
		scrollPane = new JScrollPane(txaEmTDes);
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.gridwidth = 2;
		gbc_textArea.insets = new Insets(5, 5, 5, 15);
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 1;
		gbc_textArea.gridy = 1;
		getContentPane().add(scrollPane, gbc_textArea);
		
		btnSave = new JButton("Lưu");
		btnSave.setEnabled(false);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSave){
			EmployeeType emT = new EmployeeType();
			emT.setEmTId(id);
			emT.setEmTName(StringUtil.removeUnwantedSpace(txtEmTName.getText()));
			emT.setEmTDescribe(StringUtil.removeUnwantedSpace(txaEmTDes.getText()));
			EmployeeTypeDelegate  p = new EmployeeTypeDelegate();
			boolean flag = p.updatetEmT(emT, Main.userId);
			if(flag == true){
				this.setVisible(false);
				JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
							UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
				EmT_View.tblEmT.getModel().setValueAt(StringUtil.removeUnwantedSpace(txtEmTName.getText()), rowCount, 1) ;
				EmT_View.tblEmT.getModel().setValueAt(StringUtil.removeUnwantedSpace(txaEmTDes.getText()), rowCount, 2) ;
				 
			}else{
				JOptionPane.showMessageDialog(null, UtilityGUI.errorAction ,
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
			}
			
		}else if(e.getSource() == btnCancel){
			dispose();
		}
	}
}


